<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Create HTML <video> tag for a movie
 * @author Matthijs van Henten <matthijs@waag.org>
 *
 *
<video class="currentVideo" controls="true" autoplay="false" tabindex="0">
<source src="/movies/movie_5_DNA_dice_lecture_2.ogv"/>
</video>
*
 */
class Zend_View_Helper_Player{
	private $_path;

	/**
	 * This function is used as a pseudo constructor by Zend_View
	 * and may be called as a function.
	 *
	 * @param Default_Model_Movie $movie to show
	 * 		The __set method is used to parse the variables
	 *
	 * 	@return string $pager HTML code for the pager
	 */
	public function Player( $movie ){
		if( $movie instanceof Default_Model_Movie ){
			$this->_path = AT_MOVIE_PATH . '/' . $movie->filename;
		}
		else if( is_string( $movie ) ){
			$this->_path = $movie;
		}

		return $this->getPlayer();
	}

	/**
	 * Render this pager by casting it to string
	 */
	public function __toString(){
		return $this->getPlayer();
	}

	/**
	 * Explicit render of the player
	 *
	 * @return string $HTML Html code for this pager
	 */
	public function getPlayer(){
		$source  = $this->getElement( 'source', null, array(
			'src' => $this->_path
		));


		$video = $this->getElement('video', $source, array('id'	=> 'atVideo') );
		$div   = $this->getElement( 'div', $video, array('id' => 'atPlayer') );

		return $div;
	}

	/**
	 * render a single HTML string using a variable tagname, content and attributes
	 *
	 * @param string $tagName Something of a HTML tagName such as 'div'
	 * @param string $content Content to put into the tags
	 * @param array $attributes A key_value pair of attributes
	 *
	 * @return string $HTML
	 */
	public function getElement( $tagName, $content, $attributes=array() ){
		foreach( $attributes as $key => $value ){
			$attributes[$key] = sprintf('%s="%s"', $key, htmlspecialchars($value) );
		}

		return sprintf(
			"<%s%s>%s</%s>\n",
			$tagName,
			rtrim( ' ' . join(' ', $attributes) ),
			trim($content),
			$tagName
		);
	}
}
